home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / lisp.el < prev    next >
Lisp/Scheme  |  1993-06-16  |  9KB  |  282 lines

  1. ;;; lisp.el --- Lisp editing commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: lisp, languages
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; Lisp editing commands to go with Lisp major mode.
  27.  
  28. ;;; Code:
  29.  
  30. (defvar defun-prompt-regexp nil
  31.   "Non-nil => regexp to ignore, before the `(' that starts a defun.")
  32.  
  33. (defvar parens-require-spaces t
  34.   "Non-nil => `insert-parentheses' should insert whitespace as needed.")
  35.  
  36. (defun forward-sexp (&optional arg)
  37.   "Move forward across one balanced expression (sexp).
  38. With argument, do it that many times.  Negative arg -N means
  39. move backward across N balanced expressions."
  40.   (interactive "p")
  41.   (or arg (setq arg 1))
  42.   (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
  43.   (if (< arg 0) (backward-prefix-chars)))
  44.  
  45. (defun backward-sexp (&optional arg)
  46.   "Move backward across one balanced expression (sexp).
  47. With argument, do it that many times.  Negative arg -N means
  48. move forward across N balanced expressions."
  49.   (interactive "p")
  50.   (or arg (setq arg 1))
  51.   (forward-sexp (- arg)))
  52.  
  53. (defun mark-sexp (arg)
  54.   "Set mark ARG sexps from point.
  55. The place mark goes is the same place \\[forward-sexp] would
  56. move to with the same argument."
  57.   (interactive "p")
  58.   (push-mark
  59.     (save-excursion
  60.       (forward-sexp arg)
  61.       (point))
  62.     nil t))
  63.  
  64. (defun forward-list (&optional arg)
  65.   "Move forward across one balanced group of parentheses.
  66. With argument, do it that many times.
  67. Negative arg -N means move backward across N groups of parentheses."
  68.   (interactive "p")
  69.   (or arg (setq arg 1))
  70.   (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
  71.  
  72. (defun backward-list (&optional arg)
  73.   "Move backward across one balanced group of parentheses.
  74. With argument, do it that many times.
  75. Negative arg -N means move forward across N groups of parentheses."
  76.   (interactive "p")
  77.   (or arg (setq arg 1))
  78.   (forward-list (- arg)))
  79.  
  80. (defun down-list (arg)
  81.   "Move forward down one level of parentheses.
  82. With argument, do this that many times.
  83. A negative argument means move backward but still go down a level.
  84. In Lisp programs, an argument is required."
  85.   (interactive "p")
  86.   (let ((inc (if (> arg 0) 1 -1)))
  87.     (while (/= arg 0)
  88.       (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
  89.       (setq arg (- arg inc)))))
  90.  
  91. (defun backward-up-list (arg)
  92.   "Move backward out of one level of parentheses.
  93. With argument, do this that many times.
  94. A negative argument means move forward but still to a less deep spot.
  95. In Lisp programs, an argument is required."
  96.   (interactive "p")
  97.   (up-list (- arg)))
  98.  
  99. (defun up-list (arg) 
  100.   "Move forward out of one level of parentheses.
  101. With argument, do this that many times.
  102. A negative argument means move backward but still to a less deep spot.
  103. In Lisp programs, an argument is required."
  104.   (interactive "p")
  105.   (let ((inc (if (> arg 0) 1 -1)))
  106.     (while (/= arg 0)
  107.       (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
  108.       (setq arg (- arg inc)))))
  109.  
  110. (defun kill-sexp (arg)
  111.   "Kill the sexp (balanced expression) following the cursor.
  112. With argument, kill that many sexps after the cursor.
  113. Negative arg -N means kill N sexps before the cursor."
  114.   (interactive "p")
  115.   (let ((opoint (point)))
  116.     (forward-sexp arg)
  117.     (kill-region opoint (point))))
  118.  
  119. (defun backward-kill-sexp (arg)
  120.   "Kill the sexp (balanced expression) preceding the cursor.
  121. With argument, kill that many sexps before the cursor.
  122. Negative arg -N means kill N sexps after the cursor."
  123.   (interactive "p")
  124.   (kill-sexp (- arg)))
  125.  
  126. (defun beginning-of-defun (&optional arg)
  127.   "Move backward to the beginning of a defun.
  128. With argument, do it that many times.  Negative arg -N
  129. means move forward to Nth following beginning of defun.
  130. Returns t unless search stops due to beginning or end of buffer.
  131.  
  132. Normally a defun starts when there is an char with open-parenthesis
  133. syntax at the beginning of a line.  If `defun-prompt-regexp' is
  134. non-nil, then a string which matches that regexp may precede the
  135. open-parenthesis."
  136.   (interactive "p")
  137.   (and arg (< arg 0) (forward-char 1))
  138.   (and (re-search-backward (if defun-prompt-regexp
  139.                    (concat "^\\s(\\|"
  140.                        "\\(" defun-prompt-regexp "\\)\\s(")
  141.                  "^\\s(")
  142.                nil 'move (or arg 1))
  143.        (progn (beginning-of-line) t)))
  144.  
  145. (defun buffer-end (arg)
  146.   (if (> arg 0) (point-max) (point-min)))
  147.  
  148. (defun end-of-defun (&optional arg)
  149.   "Move forward to next end of defun.  With argument, do it that many times.
  150. Negative argument -N means move back to Nth preceding end of defun.
  151.  
  152. An end of a defun occurs right after the close-parenthesis that matches
  153. the open-parenthesis that starts a defun; see `beginning-of-defun'."
  154.   (interactive "p")
  155.   (if (or (null arg) (= arg 0)) (setq arg 1))
  156.   (let ((first t))
  157.     (while (and (> arg 0) (< (point) (point-max)))
  158.       (let ((pos (point)) npos)
  159.     (while (progn
  160.         (if (and first
  161.              (progn
  162.               (forward-char 1)
  163.               (beginning-of-defun 1)))
  164.             nil
  165.           (or (bobp) (forward-char -1))
  166.           (beginning-of-defun -1))
  167.         (setq first nil)
  168.         (forward-list 1)
  169.         (skip-chars-forward " \t")
  170.         (if (looking-at "\\s<\\|\n")
  171.             (forward-line 1))
  172.         (<= (point) pos))))
  173.       (setq arg (1- arg)))
  174.     (while (< arg 0)
  175.       (let ((pos (point)))
  176.     (beginning-of-defun 1)
  177.     (forward-sexp 1)
  178.     (forward-line 1)
  179.     (if (>= (point) pos)
  180.         (if (beginning-of-defun 2)
  181.         (progn
  182.           (forward-list 1)
  183.           (skip-chars-forward " \t")
  184.           (if (looking-at "[;\n]")
  185.               (forward-line 1)))
  186.           (goto-char (point-min)))))
  187.       (setq arg (1+ arg)))))
  188.  
  189. (defun mark-defun ()
  190.   "Put mark at end of this defun, point at beginning.
  191. The defun marked is the one that contains point or follows point."
  192.   (interactive)
  193.   (push-mark (point))
  194.   (end-of-defun)
  195.   (push-mark (point) nil t)
  196.   (beginning-of-defun)
  197.   (re-search-backward "^\n" (- (point) 1) t))
  198.  
  199. (defun insert-parentheses (arg)
  200.   "Put parentheses around next ARG sexps.  Leave point after open-paren.
  201. No argument is equivalent to zero: just insert `()' and leave point between.
  202. This command also sometimes inserts a space before and after,
  203. depending on the surrounding characters."
  204.   (interactive "P")
  205.   (if arg (setq arg (prefix-numeric-value arg))
  206.     (setq arg 0))
  207.   (or (eq arg 0) (skip-chars-forward " \t"))
  208.   (and parens-require-spaces
  209.        (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
  210.        (insert " "))
  211.   (insert ?\()
  212.   (save-excursion
  213.     (or (eq arg 0) (forward-sexp arg))
  214.     (insert ?\))
  215.     (and parens-require-spaces
  216.      (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
  217.      (insert " "))))
  218.  
  219. (defun move-past-close-and-reindent ()
  220.   "Move past next `)', delete indentation before it, then indent after it."
  221.   (interactive)
  222.   (up-list 1)
  223.   (forward-char -1)
  224.   (while (save-excursion        ; this is my contribution
  225.        (let ((before-paren (point)))
  226.          (back-to-indentation)
  227.          (= (point) before-paren)))
  228.     (delete-indentation))
  229.   (forward-char 1)
  230.   (newline-and-indent))
  231.  
  232. (defun lisp-complete-symbol ()
  233.   "Perform completion on Lisp symbol preceding point.  That symbol is
  234. compared against the symbols that exist and any additional characters
  235. determined by what is there are inserted.
  236.    If the symbol starts just after an open-parenthesis, only symbols
  237. with function definitions are considered.  Otherwise, all symbols with
  238. function definitions, values or properties are considered."
  239.   (interactive)
  240.   (let* ((end (point))
  241.      (buffer-syntax (syntax-table))
  242.      (beg (unwind-protect
  243.           (save-excursion
  244.             (set-syntax-table emacs-lisp-mode-syntax-table)
  245.             (backward-sexp 1)
  246.             (while (= (char-syntax (following-char)) ?\')
  247.               (forward-char 1))
  248.             (point))
  249.         (set-syntax-table buffer-syntax)))
  250.      (pattern (buffer-substring beg end))
  251.      (predicate
  252.       (if (eq (char-after (1- beg)) ?\()
  253.           'fboundp
  254.         (function (lambda (sym)
  255.             (or (boundp sym) (fboundp sym)
  256.                 (symbol-plist sym))))))
  257.      (completion (try-completion pattern obarray predicate)))
  258.     (cond ((eq completion t))
  259.       ((null completion)
  260.        (message "Can't find completion for \"%s\"" pattern)
  261.        (ding))
  262.       ((not (string= pattern completion))
  263.        (delete-region beg end)
  264.        (insert completion))
  265.       (t
  266.        (message "Making completion list...")
  267.        (let ((list (all-completions pattern obarray predicate)))
  268.          (or (eq predicate 'fboundp)
  269.          (let (new)
  270.            (while list
  271.              (setq new (cons (if (fboundp (intern (car list)))
  272.                      (list (car list) " <f>")
  273.                        (car list))
  274.                      new))
  275.              (setq list (cdr list)))
  276.            (setq list (nreverse new))))
  277.          (with-output-to-temp-buffer " *Completions*"
  278.            (display-completion-list list)))
  279.        (message "Making completion list...%s" "done")))))
  280.  
  281. ;;; lisp.el ends here
  282.